<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/9
 * Time: 23:36
 */

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ReportModel  extends Model
{
    protected $table = 'think_reportform';
    protected $primaryKey = "id";
    protected $fillable = ['id','clickTime','goodsTitle','goodsId','shopName','goodsNums','goodsPrice','orderStatus','splitRatio','paymentAmount','settlementAmount','settlementTime','commissionRate','commissionAmount','orderNumber','groupName','orderType','payStatus'];
    public $timestamps = false;//关闭laravel自带更新created_at,updated_at,deleted_at的操作

    public function __construct()
    {
        parent::__construct();
        set_time_limit(900);//设置超时
        ini_set("memory_limit", "1024M");//使用内存限制
    }
    public function getExcelAllData($filePath)
    {
        //$inputFileType = 'Excel5';//写入有用
        $objPHPExcel = \PHPExcel_IOFactory::load($filePath);
        $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
        //var_dump($sheetData);
        return $sheetData;

    }
    public function getFieldExcelData($excelData,$field)
    {//返回指定字段的excel数据
        //$excelData array['A'=>'2017','B'=>'2017-11-10 11:05:50','C'=>'红蜻蜓女鞋','D'=>'12','E'=>'554210197192' ...]
        //$field  array['clickTime'=>'B','goodsTitle'=>'C','goodsId'=>'E'....]
        //return  array["clickTime" => "2017-11-10 11:05:50","goodsTitle" => "红蜻蜓女鞋","goodsId" => "554210197192","shopName" => "红蜻蜓品牌旗舰店","goodsNums" => "1"....]
        $item = [];
        $data = [];
        foreach($excelData as $k=>$v)
        {
            foreach($field as $key=>$val)
            {
                $item[$key] = $v[$val];

            }
            if($item['settlementTime']=='')
            {//订单付款空必须转为0000-00-00 00:00:00
                //如果0000-00-00 00:00:00插入不到数据库，可以设置成null（和数据库严格模式有关）
                $item['settlementTime'] = null;
            }
            $item['payStatus'] = '0';
            $data[] = $item;

        }
        return $data;

    }
    public function getExcelData($filePath)
    {//一个测试的方法
        $inputFileType = 'Excel5';
        $objPHPExcel = \PHPExcel_IOFactory::load($filePath);

        // 读取规则
        $sheet_read_arr = array();
        $sheet_read_arr["Page1"] = array("A", "K", "L", "S", "M", "T");
        //$sheet_read_arr["sheet2"] = array("A","B","C","D","F");

        // 循环所有的页
        foreach ($sheet_read_arr as $key => $val) {
            $currentSheet = $objPHPExcel->getSheetByName($key);// 通过页名称取得当前页
            $row_num = $currentSheet->getHighestRow();// 当前页行数
            $result = array();

            // 循环从第二行开始，第一行往往是表头
            for ($i = 1; $i <= $row_num; $i++) {
                $cell_values = array();
                foreach ($val as $cell_val) {
                    $address = $cell_val . $i;// 单元格坐标

                    // 读取单元格内容
                    $cell_values[] = $currentSheet->getCell($address)->getFormattedValue();
                }

                // 看看数据
                $result[] = $cell_values;
            }
            return $result;
        }
    }
    public function insertData($data)
    {//写入多条数据
        $res = self::insert($data);
        if($res==null)
        {
            return false;
        }
        else{
            return true;
        }

    }
    public function createData($data)
    {//写入一条数据
        $res = self::create($data);
        if($res==null)
        {
            return false;
        }
        else{
            return true;
        }
    }
    public function updateData($data)
    {//更新数据

        //$report = self::firstOrCreate(['orderNumber'=>$data['orderNumber']]);
        //dd($report);
        //$res = $report->update($data);

        $res = self::where(['orderNumber'=>$data['orderNumber']])->update($data);
        if($res)
        {
            return true;
        }
        else{
            return false;
        }


    }
    public function getinfo($orderNumber)
    {

    }
    public function isExist($orderNumber)
    {
        $res = self::where(['orderNumber'=>$orderNumber])->select()->get()->toArray();
        //dd($res);
        if($res==null || empty($res) || $res=='')
        {
            return false;
        }
        else{
            return true;
        }
    }
    public function delData()
    {//删除操作，删除2天前的数据


    }
    public function listData()
    {//获得列表

        $res = self::where('id','>','1')->select()->orderBy('clickTime','desc')->paginate(20);
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function paymentView()
    {//排组列出要支付的信息（按群主）
        $res = self::where('payStatus','=','1')->select()->orderBy('clickTime','desc')->get();
        //$select = DB::raw('goodsTitle as t,groupName as n,orderNumber as o,count(settlementAmount) as settlementAmount');
        //$res = self::where('payStatus','=','1')->select($select)->groupBy(['groupName','orderNumber','goodsTitle','groupName'])->get();
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function getGroupByGroupName()
    {//把所有的群名字列出来
        $res = self::where('payStatus','=','1')
        ->select('groupName')
        ->groupBy(['groupName'])->get();
        if($res)
        {
            return $res;
        }
            return false;
    }
    public function upeditPayedInfo($id,$qun)
    {//支付信息更改
        $res=self::where(['id'=>$id,'groupName'=>$qun])->update(['payStatus'=>2]);
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function getOneInfo($id)
    {
        $res=self::where(['id'=>$id])->first();
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function getOneInfoByOrderID($orderid)
    {//用订单编号来查找信息
        $res=self::where(['orderNumber'=>$orderid,'payStatus'=>1])->first();
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function insertBackupInfo($data)
    {//备份一下支付信息表think_paystatus
        //$bool=DB::table("vipinfo")->insert(['vip_ID'=>6,'vip_name'=>'zls','vip_type'=>"出行",'vip_fenshu'=>800]);
        $res=DB::table("think_paystatus")
            ->insert(['goodsId'=>$data['goodsId'],'goodsTitle'=>$data['goodsTitle'],'orderNumber'=>$data['orderNumber'],'commissionAmount'=>$data['commissionAmount'],'groupName'=>$data['groupName'],'payStatus'=>'2','payTime'=>date("Y-m-d H:i:s")]);
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function isOneBackupInfo($data)
    {//判断备份表有没有重复
        $res=DB::table("think_paystatus")->where('orderNumber','=',$data['orderNumber'])->get();
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function isPayed($data)
    {//在数据中获取订单编号，来判断此条信息有没有已经支付了
        $orderid = $data['orderNumber'];
        $res = $this->getOneInfoByOrderID($orderid);
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function getPaymentRecord()
    {//获取付款记录，支持群名搜索

        $res = self::where('orderStatus','=','订单结算')
            ->where('payStatus','=','2')
            ->select()->orderBy('settlementTime','desc')->paginate(2000);
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function getPromoterByqunId($qunid)
    {//通过qunid获取其详细信息
        $res = DB::table("think_promoter")->where("groupId","=",$qunid)->first();
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function insertOffsetScore($promoterId,$offsetScore,$instruction)
    {//积分记录表添加记录 think_exchangepoints
        $res = DB::table("think_exchangepoints")
            ->insert(['promoterId'=>$promoterId,'offsetScore'=>$offsetScore,'instruction'=>$instruction,'updateTime'=>date("Y-m-d H:i:s")]);
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function updateScore($promoterId,$offsetScore)
    {//增加积分 think_points,//如果数据库为空，增加积分，先新增记录

        if($this->isScore($promoterId))
        {
            //自增N,并修改其他字段
            $res=DB::table("think_points")->where('promoterId',$promoterId)->increment("score",$offsetScore,['signTime'=>date("Y-m-d H:i:s")]);
        }
        else
        {
            $res = DB::table("think_points")
                ->insert(['promoterId'=>$promoterId,'score'=>$offsetScore,'signTime'=>date("Y-m-d H:i:s"),'exchangePoints'=>0]);
        }
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function isScore($promoterId)
    {//用户是否已经有积分了
        $res = DB::table("think_points")->where("promoterId","=",$promoterId)->first();
        if($res)
        {
            return true;
        }
        return false;
    }







}